/*
 * Copyright Owen Watson 2009
 * 
 * This file is part of ArtNotes.
 *
 * ArtNotes is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * ArtNotes is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with ArtNotes.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.IO;
using MonoTouch.CoreLocation;
using FlickrNet;

namespace ArtNotes
{


	public class ANSetup
	{

		public ANSetup ()
		{
		}
				
		private ANDatabase loadDatabase()
		{
			ANDatabase anDB;
			
			if (!System.IO.Directory.Exists (AN_APP_OPTIONS.DOC_DIR))
				System.IO.Directory.CreateDirectory (AN_APP_OPTIONS.DOC_DIR);
			
			if (!System.IO.Directory.Exists (AN_APP_OPTIONS.THUMB_DIR))
				System.IO.Directory.CreateDirectory(AN_APP_OPTIONS.THUMB_DIR);
			
			if (System.IO.File.Exists (AN_APP_OPTIONS.DB_PATH))
			{
				Console.WriteLine ("Existing database " + AN_APP_OPTIONS.DB_NAME + " found!");
				anDB = new ANDatabase(AN_APP_OPTIONS.DB_PATH);
			}
			else
			{
				Console.WriteLine ("Existing database not found, creating tables...");
				anDB = new ANDatabase(AN_APP_OPTIONS.DB_PATH);
				anDB.CreateTable<Tag> ();
				anDB.CreateTable<Image> ();
				anDB.CreateTable<FlickrUser>();
				anDB.CreateTable<FlickrImage>();
				anDB.CreateTable<AppInfo>();
			}
			return anDB;
		}
		
		/// <summary>
		/// This will perform any required upgrades on the database.
		/// It checks the existing DB version against the version
		/// of the app specified in AN_APP_OPTIONS.
		/// If they differ, then the database will be upgraded to
		/// the level specified in the app.
		/// </summary>
		/// <param name="anDB">
		/// Handle to the artnotes database
		/// </param>
		private void upgradeDB(ANDatabase anDB)
		{
			AppInfo ai = anDB.GetAppInfo();
			
			string dbVersion = ai.Version;
			Console.WriteLine(dbVersion);
			if(!dbVersion.Equals(AN_APP_OPTIONS.APP_VERSION))
			{
				switch(dbVersion)
				{
				case "1.0":
					Console.WriteLine("ArtNotes Version : " + dbVersion);
					break;
				default:
					break;
				}
			}
		}
		
		/// <summary>
		/// Loads a pre-existing database and then
		/// checks to see if an upgrade is necessary.
		/// </summary>
		/// <returns>
		/// Handle to the artnotes database
		/// </returns>
		public ANDatabase existingDBSetup()
		{
			ANDatabase anDB = loadDatabase();
			upgradeDB(anDB);
			return anDB;
		}
		
		/// <summary>
		/// Deletes all existing images and thumbnails
		/// along with the database if they are present.
		/// If not / after this process is complete, the
		/// database is created along with an initial set
		/// of images to demonstrate the use of the application.
		/// </summary>
		/// <returns>
		/// Handle to the artnotes database
		/// </returns>
		public ANDatabase initialDBSetup()
		{
			ANDatabase anDB;
			bool resetConfirmed = false;
			
			Console.WriteLine("intialDBsetup");
			
			/*if (Directory.Exists (AN_APP_OPTIONS.DOC_DIR))
			{
				UIAlertView resetConfirm = new UIAlertView();
				resetConfirm.Title = "Reset ArtNotes";
				resetConfirm.Message = "Warning: This will reset ArtNotes to the default state.\nThis will remove ALL Notebooks and Images.";
				resetConfirm.AddButton("Reset");
				resetConfirm.AddButton("Cancel");
				resetConfirm.Clicked += delegate(object sender, UIButtonEventArgs e) {
					if(e.ButtonIndex.Equals(0))
						resetConfirmed = true;
				};
				resetConfirm.
				resetConfirm.Show();
				
				if(!resetConfirmed)
				{
					anDB = loadDatabase();
					return anDB;
				}
			}*/
			
			if (Directory.Exists (AN_APP_OPTIONS.DOC_DIR))
			{
				String[] tmpFiles;
				
				if(Directory.Exists(AN_APP_OPTIONS.THUMB_DIR))
				{
					tmpFiles = Directory.GetFiles(AN_APP_OPTIONS.THUMB_DIR);
					
					for(int i=0; i < tmpFiles.Length;i++)
					{
						File.Delete(tmpFiles[i]);
					}
				}
				else
					System.IO.Directory.CreateDirectory (AN_APP_OPTIONS.THUMB_DIR);
				
				tmpFiles = Directory.GetFiles(AN_APP_OPTIONS.DOC_DIR);
				
				for(int i=0; i < tmpFiles.Length;i++)
				{
					File.Delete(tmpFiles[i]);
				}
			}
			else
			{
				Console.WriteLine("not exists");
				System.IO.Directory.CreateDirectory (AN_APP_OPTIONS.DOC_DIR);
				System.IO.Directory.CreateDirectory (AN_APP_OPTIONS.THUMB_DIR);
			}
			
			anDB = loadDatabase();
			
			string[] defaultImages;
			string[] defaultThumbImages;
			
			defaultImages = new string[] {
				"ZoeWong5.png",
				"ZoeWong6.png",
				"ZoeWong3a.png",
				"ZoeWong3b.png",
				"southshields1.jpg",
				"southshields2.jpg",
				"valleta.jpg",
				"valleta-rw.jpg"
			};
			
			defaultThumbImages = new string[] {
				"thumb_ZoeWong5.png",
				"thumb_ZoeWong6.png",
				"thumb_ZoeWong3a.png",
				"thumb_ZoeWong3b.png",
				"thumb_southshields1.png",
				"thumb_southshields2.png",
				"thumb_valleta.png",
				"thumb_valleta-rw.png"
			};
			
			// Head Wear
			File.Copy("Images/default/Jewellery/" + defaultImages[0],AN_APP_OPTIONS.DOC_DIR + defaultImages[0]);
			File.Copy("Images/default/Jewellery/" + defaultImages[1],AN_APP_OPTIONS.DOC_DIR + defaultImages[1]);
			
			File.Copy("Images/default/Jewellery/thumbnails/" + defaultThumbImages[0],AN_APP_OPTIONS.THUMB_DIR + defaultThumbImages[0]);
			File.Copy("Images/default/Jewellery/thumbnails/" + defaultThumbImages[1],AN_APP_OPTIONS.THUMB_DIR + defaultThumbImages[1]);
			
			// Misc Wear
			File.Copy("Images/default/Jewellery/" + defaultImages[2],AN_APP_OPTIONS.DOC_DIR + defaultImages[2]);
			File.Copy("Images/default/Jewellery/" + defaultImages[3],AN_APP_OPTIONS.DOC_DIR + defaultImages[3]);
			
			File.Copy("Images/default/Jewellery/thumbnails/" + defaultThumbImages[2],AN_APP_OPTIONS.THUMB_DIR + defaultThumbImages[2]);
			File.Copy("Images/default/Jewellery/thumbnails/" + defaultThumbImages[3],AN_APP_OPTIONS.THUMB_DIR + defaultThumbImages[3]);
			
			// South Shields
			File.Copy("Images/default/Places/" + defaultImages[4],AN_APP_OPTIONS.DOC_DIR + defaultImages[4]);
			File.Copy("Images/default/Places/" + defaultImages[5],AN_APP_OPTIONS.DOC_DIR + defaultImages[5]);
			
			File.Copy("Images/default/Places/thumbnails/" + defaultThumbImages[4],AN_APP_OPTIONS.THUMB_DIR + defaultThumbImages[4]);
			File.Copy("Images/default/Places/thumbnails/" + defaultThumbImages[5],AN_APP_OPTIONS.THUMB_DIR + defaultThumbImages[5]);
			
			// Malta
			File.Copy("Images/default/Places/" + defaultImages[6],AN_APP_OPTIONS.DOC_DIR + defaultImages[6]);
			File.Copy("Images/default/Places/" + defaultImages[7],AN_APP_OPTIONS.DOC_DIR + defaultImages[7]);
			
			File.Copy("Images/default/Places/thumbnails/" + defaultThumbImages[6],AN_APP_OPTIONS.THUMB_DIR + defaultThumbImages[6]);
			File.Copy("Images/default/Places/thumbnails/" + defaultThumbImages[7],AN_APP_OPTIONS.THUMB_DIR + defaultThumbImages[7]);
			
			// ... Initial Database Load ...
		
			Image[] defaultDBImages;
			defaultDBImages = new Image[] {
				new Image(){FileName = defaultImages[0], ThumbFileName = defaultThumbImages[0], Title = "Head Wear 1", Description = "Head Wear 1", Author = "Zoe Wong", Lattitude = 53.800651, Longitude = -4.064941},
				new Image(){FileName = defaultImages[1], ThumbFileName = defaultThumbImages[1], Title = "Head Wear 2", Description = "Head Wear 2", Author = "Zoe Wong", Lattitude = 53.573725, Longitude = -2.436157},
				new Image(){FileName = defaultImages[2], ThumbFileName = defaultThumbImages[2], Title = "Misc 1", Description = "Misc Wear 1", Author = "Zoe Wong", Lattitude = 53.578404, Longitude = -2.428966},
				new Image(){FileName = defaultImages[3], ThumbFileName = defaultThumbImages[3], Title = "Misc 2", Description = "Misc Wear 2", Author = "Zoe Wong", Lattitude = 53.574865, Longitude = -2.424744},
				
				new Image(){FileName = defaultImages[4], ThumbFileName = defaultThumbImages[4], Title = "South Sheilds 1", Description = "South Shields at Sunset", Author = "Dave Hagan", Lattitude = 54.997177, Longitude = -1.407602},
				new Image(){FileName = defaultImages[5], ThumbFileName = defaultThumbImages[5], Title = "South Sheilds 2", Description = "South Shields at Sunset", Author = "Dave Hagan", Lattitude = 54.995275, Longitude = -1.406025},
				new Image(){FileName = defaultImages[6], ThumbFileName = defaultThumbImages[6], Title = "Valleta", Description = "Overlooking the Harbour", Author = "Dave Hagan", Lattitude = 35.908309, Longitude = 14.506529},
				new Image(){FileName = defaultImages[7], ThumbFileName = defaultThumbImages[7], Title = "Valleta", Description = "Rainbow Warrior in Valletta Harbour", Author = "Dave Hagan", Lattitude = 35.889624, Longitude = 14.517735},
			};
			
			Tag[] jewelleryTag = new Tag[] { new Tag() { TagName = "Jewellery"} };
			Tag[] placesTag = new Tag[] { new Tag() { TagName = "Places"} };
			
			for(int i = 0; i < defaultDBImages.Length; i++)
			{
				if (i < 4)
					anDB.CreateImage(defaultDBImages[i],jewelleryTag);
				else if (i < 8)
					anDB.CreateImage(defaultDBImages[i],placesTag);
				else
					anDB.CreateImage(defaultDBImages[i]);
			}

			AppInfo appInfo = new AppInfo();
			appInfo.Version = AN_APP_OPTIONS.APP_VERSION;
			anDB.CreateAppInfo(appInfo);
			
			// ... End of Initial Database Load...
			
			return anDB;
		}
		
		/// <summary>
		/// Starts the locatio nmanager to enable access to the GPS
		/// from within the application.
		/// </summary>
		/// <returns>
		/// A <see cref="CLLocationManager"/>
		/// </returns>
		public CLLocationManager startGPS()
		{
			CLLocationManager locationManager;
			locationManager = new CLLocationManager();
			locationManager.StartUpdatingLocation();
			return locationManager;
		}
		
		/// <summary>
		/// Configures access to Flickr using the API
		/// key specified in AN_APP_OPTIONS.
		/// </summary>
		/// <returns>
		/// A <see cref="Flickr"/>
		/// </returns>
		public Flickr intiFlickr()
		{
			string apikey = AN_APP_OPTIONS.AN_FLICKR_APIKEY;
			string secret = AN_APP_OPTIONS.AN_FLICKR_SECRET;
					
			Flickr flickr = new Flickr();
			flickr.ApiKey = apikey;
			flickr.ApiSecret = secret;
			
			return flickr;
		}
	}
}







